package com.marktotop.controller.system;


import com.marktotop.entity.system.Menu;
import com.marktotop.error.SystemException;
import com.marktotop.response.ResultBean;
import com.marktotop.service.IMenuService;
import com.marktotop.vo.system.MenuNodeVO;
import com.marktotop.vo.system.MenuVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author MarkToTop
 * @since 2021-07-12
 */
@RestController
@RequestMapping("/system/menus")
@Api(tags = "系统模块-菜单相关接口")
public class MenuController {

    @Autowired
    private IMenuService menuService;


    /**
     * 加载菜单树
     *
     * @return
     */
    @ApiOperation(value = "加载菜单树", notes = "获取所有菜单树，以及展开项")
    @RequestMapping(value = "/tree",method = RequestMethod.GET)
    public ResultBean menuTrees() {
        List<MenuNodeVO> menuTree = menuService.findMenuTree();
        List<Long> ids = menuService.findOpenIds();
        Map<String, Object> map = new HashMap<>();
        map.put("tree", menuTree);
        map.put("open", ids);
        return ResultBean.ok().message("加载菜单树成功!").data("menus",map);
    }


    /**
     * 新增菜单/按钮
     *
     * @return
     */
    @ApiOperation(value = "新增菜单",notes = "新增菜单/按钮")
    @RequiresPermissions({"menu:add"})
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ResultBean addMenus(@RequestBody @Validated MenuVO menuVO) {
        Menu node = menuService.addMenus(menuVO);
        Map<String, Object> map = new HashMap<>();
        map.put("id", node.getId());
        map.put("menuName", node.getMenuName());
        map.put("children", new ArrayList<>());
        map.put("icon", node.getIcon());
        return ResultBean.ok().data("result",map);
    }


    /**
     * 删除菜单/按钮
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除菜单", notes = "根据id删除菜单节点")
    @RequiresPermissions({"menu:delete"})
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    public ResultBean delete(@PathVariable Long id){
        if (!menuService.removeById(id)){
            return ResultBean.error().message("删除菜单/按钮失败!");
        }
        return ResultBean.ok().message("删除菜单/按钮成功!");
    }


    /**
     * 菜单详情
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "菜单详情", notes = "根据id编辑菜单，获取菜单详情")
    @RequiresPermissions({"menu:edit"})
    @RequestMapping(value = "/edit/{id}",method = RequestMethod.GET)
    public ResultBean editMenu(@PathVariable Long id) throws SystemException {
        MenuVO menuVO = menuService.editMenu(id);
        return ResultBean.ok().data("result",menuVO);
    }


    /**
     * 更新菜单
     *
     * @param id
     * @param menuVO
     * @return
     */
    @ApiOperation(value = "更新菜单", notes = "根据id更新菜单节点")
    @RequiresPermissions({"menu:update"})
    @RequestMapping(value = "/update/{id}",method = RequestMethod.PUT)
    public ResultBean update(@PathVariable Long id, @RequestBody @Validated MenuVO menuVO) throws SystemException {
        menuService.updateMenu(id, menuVO);
        return ResultBean.ok();
    }


}
